import random
import time

from PyGameAutoAndroid import *


def wait(t):
    delta = t/10
    time.sleep(random.uniform(t, t+delta))


def find_color_click(color, x, y, cx=None, cy=None):
    if cx is None:
        cx = x
    if cy is None:
        cy = y
    while True:
        ret = xp.matchColor(color, x, y)
        if not ret:
            wait(0.1)
            continue
        wait(0.3)
        xp.tap(cx, cy, 0.1)
        break


def find_no_color_click(color, x, y, cx=None, cy=None):
    if cx is None:
        cx = x
    if cy is None:
        cy = y
    while True:
        ret = xp.matchColor(color, x, y)
        if ret:
            wait(0.1)
            continue
        wait(0.3)
        xp.tap(cx, cy)
        break


def open_map():
    xp.console("尝试开启地图")
    # find_color_click("#E2D57E", 1077, 140, cx=471, cy=611)
    while True:
        ret = xp.matchColor("#FFFFFF", 390, 578)
        if not ret:
            wait(0.1)
            continue
        wait(1)
        xp.tap(471, 611)
        break


def goto_town():
    xp.console("尝试进城")
    find_color_click("#BBB652", 629, 358, cx=584, cy=457)
    find_color_click("#2D637B", 1053, 657)
    find_color_click("#2D6279", 797, 496)


def heal_self():
    xp.console("尝试回血")
    find_color_click("#FFFFFF", 644, 172)
    find_color_click("#ECE5DD", 673, 280)
    wait(1)
    while True:
        ret = xp.matchColor("#E2D57E", 1081, 137)
        if ret:
            wait(1)
            xp.tap(471, 611)
            break
        else:
            wait(0.1)
            xp.tap(796, 506)
            continue
    xp.console("尝试去野外")
    find_color_click("#E9E9E9", 1236, 21, cx=693, cy=281)
    find_color_click("#2D637B", 1052, 659)
    find_color_click("#2C627B", 797, 498)


def moving():
    xp.swipe(1200, 700, 1000, 700)
    wait(0.5)
    xp.swipe(1000, 700, 1200, 700)
    wait(0.5)


def is_battle():
    ret = xp.matchColor("#FFFFFF", 1077, 627)
    if ret:
        return True
    else:
        return False


def battle_sequence():
    find_color_click("#548654", 1074, 79)
    wait(0.3)
    find_color_click("#6F6F6F", 919, 564, cx=714, cy=516)
    wait(0.5)
    find_color_click("#548654", 1069, 221)
    wait(0.3)
    find_color_click("#6F6F6F", 919, 564, cx=714, cy=516)
    wait(0.5)
    find_color_click("#548654", 1084, 363)
    wait(0.3)
    find_color_click("#6F6F6F", 919, 564, cx=714, cy=516)
    wait(0.5)
    find_color_click("#548654", 1099, 507)
    wait(0.3)
    find_color_click("#6F6F6F", 919, 564, cx=714, cy=516)
    wait(0.5)
    find_color_click("#FFFFFF", 1083, 643, cx=864, cy=643)
    find_color_click("#FFFFFF", 1083, 643)

    while True:
        ret = xp.matchColor("#000000", 756, 357)
        if not ret:
            wait(0.1)
            xp.tap(756, 357)
            continue
        break


while True:
    count = 0
    req_init = True
    while True:
        if count == 0 and req_init:
            find_color_click("#E2D57E", 1080, 137)
            find_color_click("#E1E1E1", 798, 262)
            wait(3)
            req_init = False
        if count == 5:
            break
        moving()
        if is_battle() is True:
            battle_sequence()
            count += 1
        else:
            continue
    open_map()
    goto_town()
    heal_self()
